package com.sy.controller.finance;

import com.sy.bean.finance.FinanceSubject;
import com.sy.common.bean.ResultBean;
import com.sy.service.finance.FinanceSubjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/finance/subject")
@Api(tags = "finance-收支科目管理")
public class FinanceSubjectController extends MyFinanceBaseController {

    @Autowired
    FinanceSubjectService financeSubjectService;

    @GetMapping("/page")
    @ApiOperation("查询科目信息(分页)")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页码",name = "page",required = true,dataTypeClass = Integer.class),
            @ApiImplicitParam(value = "每页记录数",name = "pageSize",required = true,dataTypeClass = Integer.class)
    })
    public ResultBean query(Integer page,Integer pageSize){
        Map params =new HashMap<>();
        params.put("page",page);
        params.put("pageSize",pageSize);
        return selectResult(financeSubjectService.query(params));
    }

    @PostMapping("/")
    @ApiOperation("新增收支科目")
    public ResultBean save(@RequestBody FinanceSubject subject){
        return saveResult(financeSubjectService.save(subject));
    }

    @PutMapping("/{subject_id}")
    @ApiOperation("修改备注信息(根据科目id)")
    public ResultBean updateNote(@PathVariable("subject_id") Integer id,String note){
        int affect = 0;
        if (id != null){
            FinanceSubject subject = new FinanceSubject();
            subject.setId(id);
            subject.setNote(note);
            affect = financeSubjectService.update(subject);
        }
        return updateResult(affect);
    }

    @DeleteMapping("/{subject_id}")
    @ApiOperation("删除科目(根据id)")
    public ResultBean delete(@PathVariable("subject_id") Integer id){
        int affect = 0;
        if (id != null){
            affect = financeSubjectService.delete(id);
        }
        return deleteResult(affect);
    }
}
